//
//  LS_HeadViewTableView.m
//  breadPro
//
//  Created by 刘文超 on 2019/7/16.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "LS_HeadViewTableView.h"
#import "SDCycleScrollView.h"
#import "UUMarqueeView.h"
#import "LS_FindViewModel.h"
@interface LS_HeadViewTableView()<SDCycleScrollViewDelegate,UUMarqueeViewDelegate>
@property(nonatomic,strong)SDCycleScrollView *cycleScrollView;
@property(nonatomic,strong)UIView *tempV;
@property(nonatomic,strong)UIImageView *logogImageView1;
@property(nonatomic,strong)UIImageView *leftImage;
@property(nonatomic,strong)UUMarqueeView *customMarqueeView;
@property(nonatomic,copy)NSArray *customMarqueeViewData;
@property(nonatomic,strong)NSDictionary *ls_dict;//所有数据
@property(nonatomic,strong)LS_FindViewModel *ls_viewModel;
@end
@implementation LS_HeadViewTableView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
    }return self;
}
-(void)setBannerData:(RACSignal *)bannerData
{
    _bannerData = bannerData;
    
    [bannerData subscribeNext:^(id x) {
        
        NSMutableArray *ar = @[].mutableCopy;
        [x enumerateObjectsUsingBlock:^(NSDictionary  * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [ar addObject:kFORMAT(@"%@",obj[@"img"])];
        }];
        
        self.cycleScrollView.imageURLStringsGroup = ar;
    }];
  
}
-(void)setNewsData:(RACSignal *)newsData
{
    _newsData = newsData;
    [newsData subscribeNext:^(id x) {
        self.customMarqueeViewData = (NSArray*)x;
        [self.customMarqueeView reloadData];
    }];
    
}
-(void)setHeadData:(RACSignal *)headData
{
    _headData = headData;
    [headData subscribeNext:^(id x) {
        NSLog(@"---------%@",x);
        NSDictionary *dict = x;
        //1.赋值banner图
        NSMutableArray *ar = @[].mutableCopy;
        [dict[FoundBannerkey] enumerateObjectsUsingBlock:^(NSDictionary  * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [ar addObject:kFORMAT(@"%@",obj[@"img"])];
        }];
        self.cycleScrollView.imageURLStringsGroup = ar;
        //赋值跑马灯数据
        
        self.customMarqueeViewData = (NSArray*)x[FoundNewskey];
        [self.customMarqueeView reloadData];
        
    }];
}
-(void)ls_headViewSignal:(RACSignal *)signal WithBackModel:(id)ViewModel;
{
    //1.获取数据赋值
    [signal subscribeNext:^(id x) {
        NSLog(@"---------%@",x);
        NSDictionary *dict = x;
        self.ls_dict  = dict;
        //1.赋值banner图
        NSMutableArray *ar = @[].mutableCopy;
        [dict[FoundBannerkey] enumerateObjectsUsingBlock:^(NSDictionary  * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [ar addObject:kFORMAT(@"%@",obj[@"img"])];
        }];
        self.cycleScrollView.imageURLStringsGroup = ar;
        //赋值跑马灯数据
        
        self.customMarqueeViewData = (NSArray*)x[FoundNewskey];
        [self.customMarqueeView reloadData];
    }];
    //2.主数据
    self.ls_viewModel = ViewModel;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.tempV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.right.offset(-10);
        make.height.offset(80);
        make.top.equalTo(self.cycleScrollView.mas_bottom).offset(10);
    }];
    [self.logogImageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tempV.mas_left).offset(5);
        make.top.equalTo(self.tempV.mas_top).offset(5);
        make.right.equalTo(self.tempV.mas_right).offset(-5);
        make.height.offset(70);
    }];
    
    [self.leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.logogImageView1.mas_centerY).offset(0);
        make.left.mas_equalTo(self.logogImageView1.mas_left).offset(20);
    }];

    
    
}
-(SDCycleScrollView *)cycleScrollView
{
    return LS_LAZY(_cycleScrollView, ({
        SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, ScreenWidth, 200) delegate:self placeholderImage:LS_Getimage(@"poi_bg_placeholder")];
        cycleScrollView.backgroundColor=[UIColor whiteColor];
        cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
        cycleScrollView.layer.masksToBounds = YES;
        cycleScrollView.autoScrollTimeInterval = 4.0;
        cycleScrollView.delegate=self;
        [self addSubview:cycleScrollView];
        cycleScrollView; 
    }));
}
-(UUMarqueeView *)customMarqueeView
{
    return LS_LAZY(_customMarqueeView, ({
        UUMarqueeView *customMarqueeView = [[UUMarqueeView alloc] initWithFrame:CGRectMake(100*ls_proW,IS_IPHONE_5 ? 20 : 15, ScreenWidth-140*ls_proW, IS_IPHONE_5 ? 40 : 50)];
        customMarqueeView.delegate = self;
        customMarqueeView.timeIntervalPerScroll = 3.0f;
        customMarqueeView.timeDurationPerScroll = 1.0f;
        customMarqueeView.touchEnabled = YES;
        customMarqueeView.backgroundColor =  [UIColor whiteColor];
        [self.tempV addSubview:customMarqueeView];
        customMarqueeView;
    }));
}
-(UIView *)tempV
{
    return LS_LAZY(_tempV, ({
        UIView *tempV = [UIView new];
        [tempV setBackgroundColor:[UIColor whiteColor]];
        tempV.layer.cornerRadius=10;
        tempV.layer.masksToBounds=YES;
        [self addSubview:tempV];
        tempV;
    }));
}
-(UIImageView *)logogImageView1
{
    return LS_LAZY(_logogImageView1, ({
        UIImageView *logoImageView1=[[UIImageView alloc]init];
        logoImageView1.image=LS_Getimage(@"bgLunbo");
        logoImageView1.contentMode=UIViewContentModeScaleAspectFill;
        [self.tempV addSubview:logoImageView1];
        logoImageView1;
    }));
}
-(UIImageView *)leftImage
{
    return LS_LAZY(_leftImage, ({
       UIImageView *leftImage = [[UIImageView alloc]initWithImage:LS_Getimage(@"home_announcement")];
        [leftImage setBackgroundColor:[UIColor clearColor]];
        [self.logogImageView1 addSubview:leftImage];
        leftImage;
    }));
}

-(NSArray *)customMarqueeViewData
{
    return LS_LAZY(_customMarqueeViewData, @[]);
}
-(NSDictionary *)ls_dict
{
    return LS_LAZY(_ls_dict,@{});
}
#pragma mark - UUMarqueeViewDelegate跑马灯的代理实现
- (NSUInteger)numberOfVisibleItemsForMarqueeView:(UUMarqueeView*)marqueeView
{
    return 1;
}
- (NSArray*)dataSourceArrayForMarqueeView:(UUMarqueeView*)marqueeView
{
    return self.customMarqueeViewData;
}
- (void)createItemView:(UIView*)itemView forMarqueeView:(UUMarqueeView*)marqueeView
{
    
    UILabel *content = [[UILabel alloc] initWithFrame:CGRectMake(6.0f, 0.0f, CGRectGetWidth(itemView.bounds) - 5.0f - 20.0f, CGRectGetHeight(itemView.bounds))];
    content.font = [UIFont systemFontOfSize:13];
    content.tag = 1001;
    [itemView addSubview:content];
}
- (void)updateItemView:(UIView*)itemView withData:(id)data forMarqueeView:(UUMarqueeView*)marqueeView
{
    UILabel *content = [itemView viewWithTag:1001];
    content.textColor = [UIColor blackColor];
    if ([data isKindOfClass:[NSDictionary class]]) {
        content.text = [data objectForKey:@"article_title"];
    }else if ([data isKindOfClass:[NSString class]]){
        content.text = data;
    }
    
    
}

- (void)didTouchItemViewAtIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView*)marqueeView
{
    
    [self.ls_viewModel.didMarqueeCommand execute:self.customMarqueeViewData[index]];
    
}
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSArray *arr =self.ls_dict[FoundBannerkey];
    
    [self.ls_viewModel.didBannerCommand execute:arr[index]];
    
}

@end
